<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Order;

/**
 * OrderSearch represents the model behind the search form of `backend\models\Order`.
 */
class OrderSearch extends Order
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'uid', 'ctime', 'utime', 'order_status', 'supply_status', 'payment_status', 'account_status', 'deleted', 'trip_id', 'fund_amount'], 'integer'],
            [['order_id', 'room_ids', 'contact', 'ext', 'payment', 'email', 'phone', 'use_protect', 'message'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Order::find();

        // add conditions that should always apply here

        $request=Yii::$app->request;
        if(!$request->getQueryParam('sort')){
            $request->setQueryParams(['sort'=>'-ctime']);
        }
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'uid' => $this->uid,
            'ctime' => $this->ctime,
            'utime' => $this->utime,
            'order_status' => $this->order_status,
            'supply_status' => $this->supply_status,
            'payment_status' => $this->payment_status,
            'account_status' => $this->account_status,
            'deleted' => $this->deleted,
            'trip_id' => $this->trip_id,
            'fund_amount' => $this->fund_amount,
        ]);

        $query->andFilterWhere(['like', 'order_id', $this->order_id])
            ->andFilterWhere(['like', 'room_ids', $this->room_ids])
            ->andFilterWhere(['like', 'contact', $this->contact])
            ->andFilterWhere(['like', 'ext', $this->ext])
            ->andFilterWhere(['like', 'payment', $this->payment])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'use_protect', $this->use_protect])
            ->andFilterWhere(['like', 'message', $this->message]);

        return $dataProvider;
    }
}
